<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="所属品牌" prop="brandId">
        <el-select v-model="form.brandId" placeholder="请选择品牌">
          <el-option 
            v-for="brand in brandList" 
            :key="brand.id" 
            :label="brand.brandName" 
            :value="brand.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车系名称" prop="seriesName">
        <el-input v-model="form.seriesName" placeholder="请输入车系名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addSeries } from "@/api/car/series"
import { listBrand } from "@/api/car/brand"

export default {
  name: "SeriesAdd",
  data() {
    return {
      form: { 
        brandId: null, 
        seriesName: '' 
      },
      brandList: [], // 存储品牌列表
      // 表单校验
      rules: {
        brandId: [
          { required: true, message: "请选择所属品牌", trigger: "change" }
        ],
        seriesName: [
          { required: true, message: "车系名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // 页面加载时查询所有品牌
    this.getBrandList();
  },
  methods: {
    getBrandList() {
      // 调用品牌列表接口
      listBrand().then(response => {
        this.brandList = response.rows;
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addSeries(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.$router.push('/car/series');
          });
        }
      });
    },
    cancel() {
      this.$router.push('/car/series');
    }
  }
};
</script>
